<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>权限模块-用户管理</title>

<script type="text/javascript">

    // 表单提交的路径
    var submitPath = "<%=basePath%>auth/user/";
    var jsBase = "<%=basePath%>";
	// 之前选中的表格行
	var prevSelected = null;

	// 增删改查
	function operation(opt) {
		switch(opt){
		case "add":
			location.href='<%=basePath %>auth/user/toadd.do';
			break;
		case "mod":
			if(prevSelected == null){
				alert("请先在列表中选中要修改的项");
			}else{
				location.href='<%=basePath %>auth/user/tomod.do?pkUserinfo='+$(prevSelected).find("td:nth-child(1)").text();
			}			
			break;
		case "delete":
			if(prevSelected == null){
				alert("请先在列表中选中要删除的项");
			}else{
				if(confirm("确认要删除吗？")){
					location.href='<%=basePath %>auth/user/delete.do?pkUserinfo='+$(prevSelected).find("td:nth-child(1)").text();
				}
			}
			break;
		case "restpwd":
			if(prevSelected == null){
				alert("请先在列表中选中要重置的项");
			}else{
				if(confirm("确认要重置密码吗？")){
					location.href='<%=basePath %>auth/user/restpwd.do?pkUserinfo='+$(prevSelected).find("td:nth-child(1)").text();
				}
			}
			break;
		case "allotRole":
			if(prevSelected == null){
				alert("请先在列表中选中要分配角色的项");
			}else{
				location.href='<%=basePath %>auth/user/allotrole.do?pkUserinfo='+$(prevSelected).find("td:nth-child(1)").text();
			}			
			break;
		case "allotMenu":
			if(prevSelected == null){
				alert("请先在列表中选中要分配菜单的项");
			}else{
				location.href='<%=basePath %>auth/user/allotmenu.do?pkUserinfo='+$(prevSelected).find("td:nth-child(1)").text();
			}			
			break;
		default:
			alert("未知的操作，或脚本出错");
		}
	}
	// 选中行数据
	function bindToForm(row) {
		if (prevSelected != null) {
			$(prevSelected).removeAttr("style");// 恢复样式为默认样式
			$(prevSelected).find("input").attr("checked",false);// checkbox表示没有选中
		}
		prevSelected = row;
		$(row).css("backgroundColor", "#9c6");// 高亮提示选中行	
		$("#rounded-corner input").attr("checked",false);
		$(row).find("input").attr("checked",true);// 同时checkbox表示选中
	}
	
	
	// 显示层 部门信息管理DIV
	function showDept(sender){
		// 取得所有部门数据列表
		// 弹出DIV层
		var pos = $(sender).position();
		$.post(jsBase+"sys/dept.do?"+Math.random(), null, function(data, status){
			$("#deptList").html("");	
			$("#deptManage").toggle().css({top:pos.top+20,left:pos.left});
			data = eval(data);		
			for(var i=0; i<data.length; i++){
				var item = data[i];
				var deptHtml ="<div class='deptItem'><span>" + item.name + "</span>" 
					+ "<input type='hidden' value='"+item.pkDept+"'/>"
					+ "<a onclick='return delDept("+item.pkDept+")'  href='#'>删除</a>" 
					+ "<a onclick='return editDept(this)' href='#'>编辑</a>"
					+ "</div>";
				$("#deptList").append(deptHtml);
			}
		});
		
		return false;
	}
	// 半闭层
	function closeDept(){
		// 隐藏层
		$("#deptManage").css("display", "none");	
		// 清空原有列表
		$("#deptList").html("");
		$("#deptName").val("");
		// 刷新部门select
		$("#pkDept").empty();
		$.post(jsBase+"sys/dept.do?"+Math.random(), null, function(data, status){
			data = eval(data);
			$("#pkDept").append("<option value='' selected='selected'>全部</option>");
			for(var i=0; i<data.length; i++){
				var item = data[i];
				$("#pkDept").append("<option value='"+item.pkDept+"'>"+item.name+"</option>");
			}
		});
	}
	// 新增
	function saveDept(){
		$.post(jsBase+"sys/dept/save.do", "name="+$("#deptName").val(), function(data, status){	
			// 保存成功
			closeDept();
		});
	}
	// 删除
	function delDept(pk){
		if(confirm("确认要删除吗？")){
		$.post(jsBase+"sys/dept/delete.do?pkDept="+pk, null, function(data, status){
			var result = eval('('+eval(data)+')');
			if(result.success=='1'){
				// 删除成功操作
				closeDept();
			}else{
				alert(result.msg);
			}
			
		});
		}
		return false;
	}
	// 修改
	function editDept(link){
		var item = $(link).parent();
		// 移除原有
		$(".deptItem input[type='text']").remove();
		var intput = $("<input type='text' value='"+item.find("span").text()+"'/>");		
		item.append(intput);
		$(".deptItem input[type='text']").focus().keyup(function(event){
			// 回车提交
			if(event.keyCode == 13){
				var pkDept = $(this).parent().find("input[type='hidden']").val();
				var name = $(this).val();
				$.post(jsBase+"sys/dept/update.do", "pkDept="+pkDept+"&name="+name, function(data, status){	
					// 修改成功
					closeDept();
				});
				$(this).remove();
			}
			// ESC取消
			else if(event.keyCode == 27){
				$(this).remove();
			}			
		});
		return false;
	}
	
</script>
<style type="text/css">
#detail{
position: relative;
}
#deptManage{
position: absolute;
background-color: #fff;
display: none;
padding:4px;
border:2px solid #bed971;
min-width: 220px;
}
.title{
font-weight: bold;
color:#194227;
font-size:14px;
background-color: #99cb66;
padding:4px 6px;
}
.title span{
font-weight:normal;
float:right;
cursor: pointer;
color:#f11;
}
#deptList{
	border:1px solid #8b8;
}
.deptItem{
margin: 2px;
padding: 2px;
position:relative;
}
.deptItem a{
float:right;
margin-left: 6px;
}
.deptItem input{
position:absolute;
top:0;
left:0;
}
.deptItem:hover{
	background-color: #99cc66;
}
</style>

<%@include file="/WEB-INF/include/head-common.jsp" %>

<link rel="stylesheet" type="text/css" href="css/common.css"/>
</head>
<body>
<div id="main_container">
	<!-- 顶部信息 -->
	<%@include file="/WEB-INF/include/header.jsp" %>    
    <div class="main_content">
    	<!-- 导航菜单 -->
    	<%@include file="/WEB-INF/include/main-menu.jsp" %>
    
    <div class="center_content">  

	<!-- 左边栏内容 -->
    <div class="left_content">
    
  		<%@include file="/WEB-INF/include/sidebarmenu.jsp" %>
            
    </div>
      
    <!-- 右边框内容 -->
    <div class="right_content">
    	<div class="position">
	    	<div class="position_box">当前位置：<%@include file="/WEB-INF/include/sitemap.jsp" %></div>
	    </div>              
        
    <h2>用户管理</h2> 
	<div id="detail">
	<form id="user_form" method="post" action="auth/user.do">
		<div id="operate">
			<input type="submit" value="搜索" class="search"/> 
			<input type="button" value="新增" onclick="operation('add')" /> 
			<input type="button" value="修改" onclick="operation('mod')" /> 
			<input type="button" value="删除" onclick="operation('delete')" />
			<input type="button" value="重置密码" onclick="operation('restpwd')" />
			<input type="button" value="分配角色" onclick="operation('allotRole')" />
			<input type="button" value="分配菜单" onclick="operation('allotMenu')" />
		</div>
		<input type="hidden" id="pkUserinfo" name="pkUserinfo" />
		<span><label class="field_label">登录名：</label><input id="loginid" name="loginid" type="text" value="${param.loginid}"/></span>
		
		<span><label class="field_label">是否可用：</label>
			<select id="enabled" name="enabled">
				<option value="" selected="selected">全部</option>
				<option value="1">可用</option>
				<option value="0">禁用</option>
			</select>
		</span>
		<span>
			<label class="field_label">部门：</label>
			<select id="pkDept" name="pkDept">
			<option value="" selected="selected">全部</option>
			<c:forEach var="item" items="${depts}">
			<option value="${item.pkDept}">${item.name}</option>
			</c:forEach>
			</select>						
		</span>		
		<a href="#" onclick="return showDept(this);" style="margin-left:-16px;">编辑</a>
		</form>

		<div id="deptManage">
			<div class="title">编辑部门<span onclick="closeDept();">关闭</span></div>
			<input id="deptName" name="deptName" />
			<button onclick="saveDept()">新增</button>
			<div id="deptList">
			
			</div>
		</div>
	</div>	
	<table id="rounded-corner">
		<thead>
			<tr>
				<th class="rounded-company">编号</th>
				<th class="rounded">登录名</th>
				<th class="rounded">用户类型</th>
				<th class="rounded">是否可用</th>
				<th class="rounded">部门</th>
				<th class="rounded">电子邮件</th>
				<th class="rounded">联系电话</th>
				<th class="rounded">真实姓名</th>
				<th class="rounded">添加日期</th>
				<th class="rounded-q4">修改日期</th>
			</tr>
		</thead>
		<tbody>
			<c:forEach var="user" items="${list }">
				<tr onclick="bindToForm(this)">
					<td><input type="checkbox" />${user.pkUserinfo }</td>
					<td>${user.loginid }</td>
					<td>${user.usertype}-
					<c:choose>
						<c:when test="${user.usertype=='1'}">普通用户</c:when>
						<c:when test="${user.usertype=='2'}">管理员</c:when>
						<c:when test="${user.usertype=='3'}">超级管理员</c:when>
						<c:otherwise>错误的数据</c:otherwise>
					</c:choose>
					</td>
					<td>${user.enabled }-
					<c:choose>
						<c:when test="${user.enabled=='0'}">禁用</c:when>
						<c:when test="${user.enabled=='1'}">可用</c:when>
						<c:otherwise>错误的数据</c:otherwise>
					</c:choose>
					</td>
					<td>${user.dept.name }</td>
					<td>${user.email }</td>
					<td>${user.tellphone }</td>
					<td>${user.realname }</td>
					<td>${user.adddatetime }</td>
					<td>${user.moddatetime }</td>
				</tr>
			</c:forEach>
			<c:if test="${totalcount==0}">
			<tr>
				<td colspan="20" align="center">
					未找到相应数据
				</td>
			</tr>
			</c:if>
		</tbody>
	</table>
   	<!-- 分页条的必须参数：naviurl --> 
   	<c:set var="naviurl" value="auth/user.do" scope="request"/>
   	<!-- 引入分页条 -->  
   	<%@include file="/WEB-INF/include/pagebar.jsp" %>
     
	</div><!-- end of right content-->
            
                    
  </div>   <!--end of center content -->               
                    

    <div class="clear"></div>
    </div> <!--end of main content-->
	
    
    <div class="footer">
    
    	<div class="left_footer">IN ADMIN PANEL | Powered by <a href="http://indeziner.com">INDEZINER</a></div>
    	<div class="right_footer"><a href="http://indeziner.com"><img src="images/indeziner_logo.gif" alt="" title="" border="0" /></a></div>
    
    </div>

</div>		
</body>
</html>
